<template>
	<view class="empty-content" :class="{isfixed:fixed}">
		<h-icon class-name="empty-content-image" :src="isConnected?src:'empty/wifi'" mode="aspectFit"></h-icon>
		<view class="tips">{{isConnected?tips:'网络不可用，点击重试'}}</view>
		<slot v-if="isConnected"></slot>
		<text v-else class="btn-link" @click="retry">重新加载</text>
	</view>
</template>

<script>
	export default {
		props: {
			src: {
				type: String,
				default: 'empty/default'
			},
			tips: {
				type: String,
				default: '暂无数据'
			},
			fixed:{
				type: Boolean,
				default: true
			}
		},
		options: { styleIsolation: 'shared' },
		data() {
			return {
				isConnected: true, // 是否有网络连接
				networkType: "none", // 网络类型
			}
		},
		computed: {
		},
		mounted() {
			uni.getNetworkType({
				success: (res) => {
					this.networkType = res.networkType;
					if (res.networkType == 'none') {
						this.isConnected = false
					} else {
						this.isConnected = true
					}
				}
			})
		},
		methods: {
			// 重新检查网络
			retry() {
				uni.getNetworkType({
					success: (res) => {
						this.networkType = res.networkType;
						if (res.networkType == 'none') {
							uni.showToast({
								title: '无网络连接',
								icon: 'none',
								position: 'bottom'
							})
							this.isConnected = false;
						} else {
							uni.showToast({
								title: '网络已连接',
								icon: 'none',
								position: 'bottom'
							})
							this.isConnected = true;
						}
					}
				})
				this.$emit('retry');
			}
		}
	}
</script>

<style lang="scss">
	.empty-content {
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		padding: 70upx 0 100upx;
		border-radius: 16upx;
		background: #fff;
		&.isfixed{
			position: absolute;
			left: 20upx;
			top: 20upx;
			right: 20upx;
			bottom: 20upx;
			height: auto;
		}
		.tips{
			font-size: 24upx;
			color: #999999;
			margin: 20upx 0;
			text-align: center;
		}
		/deep/ {
			.empty-content-image{
				width: 280upx;
				height: 250upx;
			}
		}
	}
</style>
